local ninglu = fk.CreateSkill{
  name = "investiture__ninglu",
  tags = {Skill.Compulsory}
}

Fk:loadTranslationTable{
  ["investiture__ninglu"] = "佞禄",
  [":investiture__ninglu"] = "锁定技，分发起始手牌或你失去最后的明置手牌后，若你没有手牌，你摸一张牌并明置之，否则你须明置至少一张手牌。",

  ["#investiture__ninglu-show"] = "佞禄：请明置至少一张手牌",
}

local DIY = require "packages/diy_utility/diy_utility"
local TuntianUtil = require "packages/tuntian_studio/utility"

ninglu:addEffect(fk.AfterDrawInitialCards, {
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill("investiture__ninglu") and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local cards = table.filter(player:getCardIds("h"), function (c)
      return not TuntianUtil.isShown(c)
    end)
    local show = player.room:askToCards(player, {
      max_num = 999,
      min_num = 1,
      skill_name = ninglu.name,
      cancelable = false,
      pattern = tostring(Exppattern{ id = cards }),
      prompt = "#investiture__ninglu-show",
    })
    DIY.showCards(player, show)
  end
})

ninglu:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(ninglu.name) and #table.filter(player:getCardIds("h"), function (c)
      return TuntianUtil.isShown(c)
    end) == 0 then
      for _, move in ipairs(data) do
        if move.extra_data and move.extra_data.investiture__ninglu == player.id then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    if player:getHandcardNum() > 0 then
      local cards = table.filter(player:getCardIds("h"), function (c)
        return not TuntianUtil.isShown(c)
      end)
      local show = player.room:askToCards(player, {
        max_num = 999,
        min_num = 1,
        skill_name = ninglu.name,
        cancelable = false,
        pattern = tostring(Exppattern{ id = cards }),
        prompt = "#investiture__ninglu-show",
      })
      DIY.showCards(player, show)
    else
      local card = player:drawCards(1, ninglu.name)
      if player.dead then return end
      DIY.showCards(player, card)
    end
  end
})

ninglu:addEffect(fk.BeforeCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(ninglu.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if TuntianUtil.isShown(info.cardId) then
            move.extra_data = move.extra_data or {}
            move.extra_data.investiture__ninglu = player.id
          end
        end
      end
    end
  end,
})

return ninglu